/*
 * Created on 14.04.2008
 * @author Jens Anuth
 */
package albareth.frontend.slick.ui;




import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Vector2f;

import albareth.frontend.slick.SlickResources;
import frosch.slick.glider.ColorGlider;
import frosch.slick.glider.VectorGlider;
import frosch.util.glider.Easing;
import frosch.util.glider.PropertyGlider;
import frosch.util.glider.PropertyGlidingListener;




public abstract class GlidingUIElement extends UIElement
                                       implements PropertyGlidingListener
{
    private GlidingUIElementListener _listener = null;
    private VectorGlider _positionGlider;
    private ColorGlider _bgAlphaGlider;
    private Color _bgFilterColor = new Color(1f, 1f, 1f, 0f);
    private boolean _darkenBackground = false;
    private boolean _glidingInFinished = false;
    private boolean _glidingOut = false;

    
    public GlidingUIElement(int px, int py,
                            int startX, int startY,
                            Easing glideEasing, int glideDuration)
    {
        super(startX, startY);
        _positionGlider = new VectorGlider(_pos, new Vector2f(px, py), glideDuration);
        _positionGlider.setLoopMode(PropertyGlider.LOOPING_OFF);
        _positionGlider.setEasing(glideEasing);
        _positionGlider.setGlidingListener(this);

        _bgAlphaGlider = new ColorGlider(
                _bgFilterColor, new Color(1f, 1f, 1f, 1f), glideDuration
        );
        _bgAlphaGlider.setLoopMode(PropertyGlider.LOOPING_OFF);
        //_bgAlphaGlider.setGlidingListener(this);
        startGlidingIn();
    }
    

    public void startGlidingIn()
    {
        reset();
        _positionGlider.setInverted(false);
        _bgAlphaGlider.setInverted(false);

    }
    
    public void startGlidingOut()
    {
        reset();
        _glidingInFinished = true;
        _glidingOut = true;
        _positionGlider.setInverted(true);
        _bgAlphaGlider.setInverted(true);
    }
    
    public void reset()
    {
        _glidingInFinished = false;
        _glidingOut = false;
        _positionGlider.reset();
        _bgAlphaGlider.reset();
    }

    public void processUpdate(int delta, GameContainer container,
            UIManager manager) throws SlickException
    {
        super.processUpdate(delta, container, manager);
        _positionGlider.update(delta);
        _bgAlphaGlider.update(delta);
    }
    
    public void processRender(Graphics g, GameContainer container) throws SlickException
    {
        if (_darkenBackground) {
            g.drawImage(SlickResources.IMG_DARKEN, 0, 0, _bgFilterColor);
        }

        super.processRender(g, container);
        
    }

    public void glidingFinished(Object glidedObject)
    {
        if (!_glidingInFinished)
        {
            _glidingInFinished = true;
            if (_listener != null) {
                _listener.glideInFinished(this);
            }
        } else {
            if (_listener != null) {
                _listener.glideOutFinished(this);
            }
        }
        
    }

    public final boolean isGlidingInFinished()
    {
        return _glidingInFinished;
    }
    
    public final boolean isGlidingIn()
    {
        return !_glidingInFinished;
    }

    public final boolean isGlidingOut()
    {
        return _glidingOut;
    }
    
    public final boolean isGliding()
    {
        return _glidingOut || !_glidingInFinished;
    }


    public void setDarkenBackground(boolean darkenBackground)
    {
        _darkenBackground = darkenBackground;
    }

    public GlidingUIElementListener getGlidingListener()
    {
        return _listener;
    }

    public void setGlidingListener(GlidingUIElementListener listener)
    {
        _listener = listener;
    }

}
